﻿using UnityEngine;

namespace FwtUnityEngine
{
    public static class GeneralFactory
    {
        public static Mesh CreateSquareMesh()
        {
            var size = new Vector2(1, 1);
            var zero = new Vector2(0.5f, 0.5f);
            var vertices = new[]
                       {
                         new Vector3(0, 0, 0),          // 1 ___  2
                         new Vector3(0, size.y, 0),     //   |  |
                         new Vector3(size.x, size.y, 0),//   |  |
                         new Vector3(size.x, 0, 0)      // 0 ---- 3
                       };

            Vector3 shift = Vector2.Scale(zero, size);
            for (var i = 0; i < vertices.Length; i++)
                vertices[i] -= shift;

            var textureCoords = new Rect(0, 0, 1, 1);
            var uv = new[]
                         {
                             new Vector2(textureCoords.xMin, 1 - textureCoords.yMax),
                             new Vector2(textureCoords.xMin, 1 - textureCoords.yMin),
                             new Vector2(textureCoords.xMax, 1 - textureCoords.yMin),
                             new Vector2(textureCoords.xMax, 1 - textureCoords.yMax)
                         };

            var triangles = new[]
                                {
                                    0, 1, 2,
                                    0, 2, 3
                                };

            return new Mesh { vertices = vertices, uv = uv, triangles = triangles };
        }
        public static Mesh CreateSquareMesh(Rect textureCoords)
        {
            var size = new Vector2(1, 1);
            var zero = new Vector2(0.5f, 0.5f);
            var vertices = new[]
                       {
                         new Vector3(0, 0, 0),          // 1 ___  2
                         new Vector3(0, size.y, 0),     //   |  |
                         new Vector3(size.x, size.y, 0),//   |  |
                         new Vector3(size.x, 0, 0)      // 0 ---- 3
                       };

            Vector3 shift = Vector2.Scale(zero, size);
            for (var i = 0; i < vertices.Length; i++)
                vertices[i] -= shift;

            var uv = new[]
                         {
                             new Vector2(textureCoords.xMin, 1 - textureCoords.yMax),
                             new Vector2(textureCoords.xMin, 1 - textureCoords.yMin),
                             new Vector2(textureCoords.xMax, 1 - textureCoords.yMin),
                             new Vector2(textureCoords.xMax, 1 - textureCoords.yMax)
                         };

            var triangles = new[]
                                {
                                    0, 1, 2,
                                    0, 2, 3
                                };

            return new Mesh { vertices = vertices, uv = uv, triangles = triangles };
        }
        public static Mesh ChangeTextureCoords(Mesh mesh, Rect textureCoords)
        {
            var uv = new[]
                         {
                             new Vector2(textureCoords.xMin, 1 - textureCoords.yMax),
                             new Vector2(textureCoords.xMin, 1 - textureCoords.yMin),
                             new Vector2(textureCoords.xMax, 1 - textureCoords.yMin),
                             new Vector2(textureCoords.xMax, 1 - textureCoords.yMax)
                         };
            mesh.uv = uv;
            return mesh;
        }

        public static GameObject CreateSpriteObject()
        {
            var go = new GameObject();
            go.AddComponent(typeof (MeshFilter));
            go.AddComponent(typeof (MeshRenderer));
            var sprite = (Sprite2D)go.AddComponent(typeof(Sprite2D));
            sprite.Init();
            return go;
        }
    }
}
